package com.xq.springboot.aspect;

import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/19 14:40
 * @description 全局异常监控类
 */
@RestControllerAdvice // 全局通知注解、以json数据格式返回出去
public class AppExceptionAdivse {
    /**
     * 全局异常捕捉未经身份验证的返回信息
     * @return
     */
    @ExceptionHandler(value = {UnauthenticatedException.class})
    public Map<String, Object> unauthorized() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code",302);
        map.put("msg","未经身份验证，无权访问");
        return map;
    }

    /**
     * 全局异常捕捉身份已验证未经授权的返回信息
     * @return
     */
    @ExceptionHandler(value = {UnauthorizedException.class})
    public Map<String, Object> unauthorizedException() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code",301);
        map.put("msg","无权限访问");
        return map;
    }
}
